package com.compass.doctor.dialog

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import androidx.fragment.app.FragmentActivity
import com.amap.api.location.AMapLocation
import com.amap.api.maps.AMapUtils
import com.amap.api.maps.model.LatLng
import com.compass.doctor.R
import com.compass.doctor.callback.OnLocationButtonCallBack
import com.compass.doctor.databinding.DialogLocationBinding
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.ext.onClick
import com.compass.framework.helper.CompassAppHelper
import com.compass.framework.manager.AppManager
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.MedicalCareOrder
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.LogUtil
import com.compass.framework.utils.getColorFromResource

/**
 * Description： 位置弹窗
 * Date:2023/12/31 12:56
 */
class LocationDialog {
    class Builder(
        activity: FragmentActivity,
        private val aMapLocation: AMapLocation,
        private val careOrder: MedicalCareOrder,
        private val onLocationButtonCallBack: OnLocationButtonCallBack
    ) : BaseDialogFragment.Builder<Builder>(activity) {


        private val mBinding: DialogLocationBinding =
            DialogLocationBinding.inflate(LayoutInflater.from(activity))
        private var isSkewing: Boolean = false

        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenWidthPx() * 0.8).toInt())
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            mBinding.tvDialogMessage.text = aMapLocation.address
            when (careOrder.status) {
                "3" -> {
                    mBinding.tvData.text =
                        DateUtils.conversionTime(aMapLocation.time, DateUtils.FORMAT_TWO)
                    mBinding.tvDialogMessageTitle.text = "出发提示"
                }

                "4" -> {
                    mBinding.tvDialogMessageTitle.text = "到达提示"
                    var starLatLng = LatLng(aMapLocation.latitude, aMapLocation.longitude)
                    careOrder.address?.let {
                        var endLatLng = LatLng(it.getLat(), it.getLon())
//                        var endLatLng = LatLng(34.14652, 108.841896)//测试坐标
                        mBinding.tvData.text = getDistance(starLatLng, endLatLng)
                    } ?: kotlin.run {
                        mBinding.tvData.text =
                            DateUtils.conversionTime(aMapLocation.time, DateUtils.FORMAT_TWO)
                    }

                }
            }
            mBinding.tvDialogMessageCancel.onClick {
                dismiss()
            }
            mBinding.tvDialogMessageConfirm.onClick {
                if (careOrder.status == "4" && isSkewing) {
                    TipsToast.showTips("当前位置偏移，无法修改状态")
                    return@onClick
                }

                onLocationButtonCallBack.onButtonCallBack(
                    careOrder.id,
                    careOrder.status,
                    aMapLocation.latitude,
                    aMapLocation.longitude
                )
                dismiss()
            }
        }


        @SuppressLint("SuspiciousIndentation")
        private fun getDistance(startPoint: LatLng, endPoint: LatLng): String {
            isSkewing = false
            val distance = AMapUtils.calculateLineDistance(startPoint, endPoint)
            var tip: String = DateUtils.conversionTime(aMapLocation.time, DateUtils.FORMAT_TWO)
            mBinding.tvData.setTextColor(getColorFromResource(R.color.color_999999))

            val geographicalLocation =
                UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.geographicalLocation
            if (geographicalLocation == "1") {
                val verificationScope =
                    UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.verificationScope
                var scope: Int
                if (verificationScope.isNullOrEmpty()) scope = 300 else scope =verificationScope.toInt()
                if (distance >  scope) {
                    tip = "当前位置偏移，无法修改状态"
                    isSkewing = true
                    mBinding.tvData.setTextColor(getColorFromResource(R.color.color_d22c27))
                }
            }

            return tip
        }
    }
}